<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：CooperateAgency.php
 * @类功能: seo管理
 * @开发者: gys
 * @开发时间： 15-10-22
 * @版本：version 1.0
 */
namespace app\admin\controller;

use app\common\cache\common\ISeoCache;
use app\common\controller\AdminBaseController;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;

class Seo extends AdminBaseController {
    /**
     * @功能：seo列表
     * @开发者：gys
     */
    public function seo_list(){
        $page_code = IRequest::get("page_code");
        $page_name = IRequest::get("page_name");
        $remark = IRequest::get("remark");

        $seo_where['is_delete'] = IsDelete::No;
        if(!empty($page_code)) $seo_where['page_code'] = ['like',"%{$page_code}%"];
        if(!empty($page_name)) $seo_where['page_name'] = ['like',"%{$page_name}%"];
        if(!empty($remark)) $seo_where['remark'] = ['like',"%{$remark}%"];

        //获取seo列表
        $seo_list = IDb::getInstance("seo")->setDbWhere($seo_where)->sel();

        //模板渲染
        $this->assign('seo_list',$seo_list);
        $this->assign('page_code',$page_code);
        $this->assign('page_name',$page_name);
        $this->assign('remark',$remark);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：seo编辑
     * @开发者：gys
     */
    public function seo_edit(){
        //接收参数
        $seo_id = IRequest::get("seo_id",IRequest::NOT_EMPTY,"seo编号不能为空！");

        //实例化对象
        $seo_where['seo_id'] = $seo_id;
        $seo_info = IDb::getInstance("seo")->setDbWhere($seo_where)->row();

        //模板渲染
        $this->assign('seo_id',$seo_id);
        $this->assign('seo_info',$seo_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：seo编辑提交
     * @开发者：gys
     */
    public function seo_submit(){
        //接收参数
        $seo_id = IRequest::get("seo_id");
        $seo_title = IRequest::get("seo_title",IRequest::NOT_EMPTY,"seo标题不能为空！");
        $seo_keywords = IRequest::get("seo_keywords",IRequest::NOT_EMPTY,"seo关键字不能为空！");
        $seo_description = IRequest::get("seo_description",IRequest::NOT_EMPTY,"seo描述不能为空！");
        $remark = IRequest::get("remark");

        $seo_data['seo_title'] = $seo_title;
        $seo_data['seo_keywords'] = $seo_keywords;
        $seo_data['seo_description'] = $seo_description;
        $seo_data['remark'] = $remark;

        //判断是修改还是添加
        if(empty($seo_id)) {
            //添加seo编号
            $seo_id = IDb::getInstance("seo")->setDbData($seo_data)->add();
            if($seo_id === false){
                error('添加失败');
            }
        }else{
            //修改seo
            $seo_where['seo_id'] = array("eq",$seo_id);
            $seo_upd = IDb::getInstance("seo")->setDbWhere($seo_where)->setDbData($seo_data)->upd();
            if($seo_upd === false){
                error('修改失败！');
            }
        }

        $seo_info = IDb::getInstance("seo")->setDbFiled('page_code')->setDbWhere(['seo_id'=>$seo_id])->row();
        if(!empty($seo_info['page_code'])){
            //清除缓存
            ISeoCache::deleteInfo($seo_info['page_code']);
        }

        //画面跳转
        $this->success('操作成功',Url('seo_list'));
    }

}
